unit Global;

interface
uses
  Windows, SysUtils, IniFiles, Forms, Controls,BaseDataset ;

function IsValidObject(Obj: TObject): Boolean;
function IsValidClass(Cls: TClass): Boolean;
procedure ForceWriteToApplicationLog(strMessage: WideString);
function PermitFocus(Control: TWinControl): boolean;
procedure TrimAppMemorySize;
function CanUseStudentProviderComponent: Boolean; { -ogay : added }
function ShowEMRViewUtility: Boolean; { -ogay : added }
procedure InfoLog(sPath,sStaffKey:String;strMessage: WideString);{sarma for ONC WAVE-2}

var
  bDetailLog, bIniChecked: Boolean; //Raghu 20121020
  bISummary:boolean;
  LogUser:String;
  UserLevel:String;
  PayrollID:integer;
  AConStrGlobal:string;
  PayrollIDComment,strPrintType:string;

implementation

function IsValidObject(Obj: TObject): Boolean;
begin
  Result := not IsBadReadPtr(Obj, sizeof(Pointer)) and IsValidClass(Obj.ClassType) and not IsBadReadPtr(Obj, Obj.InstanceSize);
end;

function IsValidClass(Cls: TClass): Boolean;
var
  i: Integer;
begin
  for i := 0 to 99 do begin
    Result := (Cls = TObject); // note that other modules may have a different root TObject!
    if Result then Exit;
    if IsBadReadPtr(Cls, sizeof(Pointer)) then Break;
    if IsBadReadPtr(Pointer(Integer(Cls) + vmtParent), sizeof(Pointer)) then Break;
    Cls := Cls.ClassParent;
  end;
  Result := False;
end;

//Raghu 20121020

procedure ForceWriteToApplicationLog(strMessage: WideString);
var
  strLog: widestring;
  eLog: TextFile;
  strPath: string;
  fConfig: TIniFile;
  sLogFileName: string;
  sELogFileName,sStaffKey : String; //Sarma on May.10.2013
begin
  {
  if not bIniChecked then
  begin
    try
      fConfig := TInifile.Create(ExtractFilePath(Application.Exename) + 'MDoffice.ini');
      bDetailLog := fConfig.ReadBool('Debugging', 'DetailedLog', False);
      FreeAndNil(fConfig);
      bIniChecked:= True;
    except
      //
    end;
  end;

  if not bDetailLog then
     Exit;
  }//Commented by Raghu on 20121021: As per Vasu instructions we should not check Ini value for Forcelog.
  try
    if Pos('abort', LowerCase(strMessage)) > 0 then //sarma on feb.05.2013
      Exit;

    strPath := ExtractFilePath(Application.Exename);

    //Sarma on May.10.2013

    sELogFileName := 'WinRegistration_'+FormatDateTime('mmddyyyyhhnnss', Now)+'.log';

    strPath := strPath+'ErrorsLog\';

    if not DirectoryExists(strPath) then
      ForceDirectories(strPath);

    //Sarma on May.10.2013



    strLog := Format('%s'#09'%s',
      [FormatDateTime('mm/dd/yy h:nn:ssa/p', Now), strMessage]);

    AssignFile(eLog, strPath + sELogFileName );

    if FileExists(strPath + sELogFileName) then
      Append(eLog)
    else
      ReWrite(eLog);

    Writeln(eLog, strLog);
    CloseFile(eLog);
  except
    //
  end;
end;
//Raghu 20121020
//Ranjan 20121102

function PermitFocus(Control: TWinControl): boolean;
begin
  try
    result := False;

    if not (Control.Visible and Control.Enabled) then Exit;

    while Assigned(Control.Parent) do
    begin
      Control := Control.Parent;
      if not (Control.Visible and Control.Enabled) then Exit;
    end;
    result := True;
  except on ex: exception do
    begin
      ForceWriteToApplicationLog('Error in function Global.PermitFocus - ' + Ex.ClassName + '..Error details: ' + #13#10 + Ex.Message); //Ranjan 2012/11/02
    end;
  end;
end;

procedure TrimAppMemorySize;
var
  MainHandle: THandle;
begin
  try
    MainHandle := OpenProcess(PROCESS_ALL_ACCESS, false, GetCurrentProcessID);
    SetProcessWorkingSetSize(MainHandle, $FFFFFFFF, $FFFFFFFF);
    CloseHandle(MainHandle);
    Application.ProcessMessages;
  except
  end;
end;

function CanUseStudentProviderComponent: Boolean;
var IniFile : TIniFile;
begin
  result := False;
  try
    IniFile := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')) ;
    IniFile :=  TInifile.Create(ExtractFilePath(Application.ExeName)+'mdoffice.ini');
    result := IniFile.ReadInteger('ChartDesigner','UseStudentProvider',0) = 1;
  finally
    IniFile.Free;
  end;
end;

function ShowEMRViewUtility: Boolean;
var IniFile : TIniFile;
begin
  result := False;
  try
    IniFile := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')) ;
    IniFile :=  TInifile.Create(ExtractFilePath(Application.ExeName)+'mdoffice.ini');
    result := IniFile.ReadInteger('ChartDesigner','ShowEMRViewUtility',0) = 1;
  finally
    IniFile.Free;
  end;
end;
procedure InfoLog(sPath,sStaffKey:String;strMessage: WideString);{sarma for ONC WAVE-2}
var
  strLog: widestring;
  eLog: TextFile;
  strPath: string;
  fConfig: TIniFile;
  sLogFileName: string;
  sELogFileName : String; //Sarma on May.10.2013
begin
  try
    if Pos('abort', LowerCase(strMessage)) > 0 then //sarma on feb.05.2013
      Exit;

 //   strPath := ExtractFilePath(Application.Exename);
    sELogFileName := sStaffKey+'_'+FormatDateTime('mmddyyyyhhnnss', Now)+'.log';

    strPath := sPath;//strPath+'ErrorsLog\';

    if not DirectoryExists(strPath) then
      ForceDirectories(strPath);

    strLog := Format('%s'#09'%s',
      [FormatDateTime('mm/dd/yy h:nn:ssa/p', Now), strMessage]);

    AssignFile(eLog, strPath + sELogFileName );

    if FileExists(strPath + sELogFileName) then
      Append(eLog)
    else
      ReWrite(eLog);

    Writeln(eLog, strLog);
    CloseFile(eLog);
  except
    //
  end;
end;

end.
 
